export class Bezier {
    static jumpTo(p0: cc.Vec2, p2: cc.Vec2, height: number, t: number) {
        let p1 = cc.v2(p0.x + (p2.x - p0.x) / 2, p0.y + height)
        return Bezier.bezier2(p0, p1, p2, t);
    }

    // 二阶贝塞尔
    static bezier2(p0: cc.Vec2, p1: cc.Vec2, p2: cc.Vec2, t: number) {
        let t1 = (1 - t) * (1 - t);
        let t2 = 2 * t * (1 - t);
        let t3 = t * t;
        return p0.mul(t1).add(p1.mul(t2)).add(p2.mul(t3));
    }

    // 三阶贝塞尔
    static bezier3(p0: cc.Vec2, p1: cc.Vec2, p2: cc.Vec2, p3: cc.Vec2, t: number) {
        let t0 = (1 - t) * (1 - t) * (1 - t);
        let t1 = 3 * t * (1 - t) * (1 - t);
        let t2 = 3 * t * t * (1 - t);
        let t3 = t * t * t;
        return p0.mul(t0).add(p1.mul(t1)).add(p2.mul(t2)).add(p3.mul(t3));
    }

    // 四阶贝塞尔
    static bezier4(p0: cc.Vec2, p1: cc.Vec2, p2: cc.Vec2, p3: cc.Vec2, p4: cc.Vec2, t: number) {
        let t0 = (1 - t) * (1 - t) * (1 - t) * (1 - t);
        let t1 = 4 * t * (1 - t) * (1 - t) * (1 - t);
        let t2 = 4 * t * t * (1 - t) * (1 - t);
        let t3 = 4 * t * t * t * (1 - t);
        let t4 = t * t * t * t;
        return p0.mul(t0).add(p1.mul(t1)).add(p2.mul(t2)).add(p3.mul(t3)).add(p4.mul(t4));
    }
}
